/*
* 加这一层是为了提高代码的可拓展能力
* */
package org.csu.myProject.service;

import org.csu.myProject.domain.User;
import org.csu.myProject.persistence.UserDAO;
import org.csu.myProject.persistence.UserDAOImpl;

import java.util.List;

public class UserServiceImpl implements UserService{

    private UserDAO userDAO;

    private String msg;

    public UserServiceImpl()
    {
        this.userDAO = new UserDAOImpl();
    }

    @Override
    public User login(String username, String password) {
        return userDAO.findUserByUsernameAndPassword(username, password);
    }

    @Override
    public List<User> getUserList() {
        //其他业务代码，比如说将用户的密码隐藏
        return userDAO.getUserList();
    }

    @Override
    public User getUserInfo(int id) {
        return userDAO.getUserInfo(id);
    }

    @Override
    public int reegister(User user) {
        if(usernameExist(user.getUsername()))
        {
            msg = "该用户名已存在";
            return 0;
        }else{
            return userDAO.insertUser(user);
        }
    }

    @Override
    public boolean usernameExist(String username) {
        return userDAO.getUserByUsername(username);
    }

    @Override
    public String getMsg() {
        return msg;
    }
}
